﻿package com.hxh.ocean.view{

	import com.hxh.components.CustomColorPicker;
	import com.hxh.utils.Maths;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.utils.Timer;
	
	import hxh.ocean.events.*;

	public class MenuItem extends Bubble {
		
		private var _childItems:Array;
		private var _configButtons:Array;
		private var _childrenTimer:Timer;
		private var _configButtonsTimer:Timer;
		private var _colorPicker:CustomColorPicker;

		// Initialization:
		public function MenuItem(itemName:String, url:String=null) {
			var sprite:MovieClip = new BubbleView();
			super(itemName, sprite);
			_childItems = new Array();
			_configButtons = new Array();
			TargetURL = url;
			buildConfigButtons();
		}

		/********************* PUBLIC METHODS **********************/
		
		public override function set PlaySFX(v:Boolean) : void{
			super.PlaySFX = v;
			for (var i:int = 0; i < _configButtons.length; i++){
				BubbleButton(_configButtons[i]).PlaySFX = v;
			}
		}

		public function startShowChildrenTimer():void{
			_childrenTimer = new Timer(2000,1);
			_childrenTimer.addEventListener(TimerEvent.TIMER_COMPLETE, finishShowChildren);
			_childrenTimer.start();
			removeConfigButtons(null);
		}
		
		public function newChildItem(item:MenuChildItem):void {
			item.Scale=this.Scale*2/3;
			item.addEventListener(Events.CHILDITEM_OVER, childrenMouseOver);
			item.addEventListener(Events.CHILDITEM_OUT, childrenMouseOut);
			item.ParentItem = this;
			_childItems.push(item);			
		}
		
		public function getChildren():Array{
			return _childItems;
		}
		
		public function noActiveChild():Boolean{
			for (var i:int = 0; i < _childItems.length; i++){
				if (_childItems[i].IsAlive){
					return false;
				}
			}
			return true;
		}

		public function placingChildren():void {
			//First of all caculate the angles between 2 items
			var angle:Number =  2* Math.PI / _childItems.length;
			//Loop for all items
			for (var i:int = 0; i< _childItems.length; i++) {
				var item:MenuChildItem = _childItems[i];
				//Color, scale and hide
				item.BaseColor=this.BaseColor;
				
				
				//Positioning
				item.x = x+(2 * Radius)* Math.cos(angle*(i));
				item.y = y+(2 * Radius)* Math.sin(angle*(i));
				
				item.popIn();
			}
		}

		/********************* PRIVATE METHODS *********************/
		
		protected override function mouseIn(evt:MouseEvent) : void{
			super.mouseIn(evt);
			placingConfigButtons();
			if (_configButtonsTimer!=null&&_configButtonsTimer.running){
				_configButtonsTimer.reset();
			}
			FisixContainer.fixed = true;
		}
		
		protected override function mouseAway(evt:MouseEvent) : void{
			super.mouseAway(evt);
			_configButtonsTimer = new Timer(500,1);
			_configButtonsTimer.addEventListener(TimerEvent.TIMER_COMPLETE, removeConfigButtons);
			_configButtonsTimer.start();
			
			FisixContainer.fixed = false;
			FisixContainer.thrust(Maths.randomInt(-5,5), Maths.randomInt(-5,5));
			trace(mouseX+", "+mouseY);
		}

		private function placingConfigButtons():void {
			//Loop for all buttons
			for (var i:int = 0; i< _configButtons.length; i++) {
				var button:BubbleButton = _configButtons[i];
				//Positioning
				button.x = i*20;
				button.y = (Radius+10);
				addChild(button);
				button.popIn();
			}
		}
		
		private function finishShowChildren(evt:TimerEvent):void {
			dispatchEvent(new Event(Events.SHOWCHILDREN_FINISH));
			FisixContainer.fixed = false;
			FisixContainer.thrust(Maths.randomInt(-5,5), Maths.randomInt(-5,5));
		}
		
		private function childrenMouseOver(evt:Event):void {
			if (_childrenTimer.running) {
				_childrenTimer.reset();
			}
		}

		private function childrenMouseOut(evt:Event):void {
			_childrenTimer.start();
		}
		
		private function buildConfigButtons():void{
			//Button change color
			var view:ButtonView = new ButtonView();
			view.setBaseSprite(new ConfigGear());
			var btnChangeColor:BubbleButton = new BubbleButton("Change Color", view);
			_configButtons.push(btnChangeColor);
			btnChangeColor.HitArea.addEventListener(MouseEvent.MOUSE_OVER, configButtonMouseOver);
			btnChangeColor.HitArea.addEventListener(MouseEvent.MOUSE_OUT, configButtonMouseOut);
			btnChangeColor.addEventListener(Events.BUBBLE_CLICK, changeColor);
			btnChangeColor.Scale = 0.3;
			
			//Button toggle label
			view = new ButtonView();
			view.setBaseSprite(new ABCText());
			var btnToggleLabel:BubbleButton = new BubbleButton("Toggle Label", view);
			_configButtons.push(btnToggleLabel);
			btnToggleLabel.HitArea.addEventListener(MouseEvent.MOUSE_OVER, configButtonMouseOver);
			btnToggleLabel.HitArea.addEventListener(MouseEvent.MOUSE_OUT, configButtonMouseOut);
			btnToggleLabel.addEventListener(Events.BUBBLE_CLICK, toggleLabel);
			btnToggleLabel.Scale = 0.3;
		}
		
		private function removeConfigListeners(button:BubbleButton):void{
			button.HitArea.removeEventListener(MouseEvent.MOUSE_OVER, configButtonMouseOver);
			button.HitArea.removeEventListener(MouseEvent.MOUSE_OUT, configButtonMouseOut);
		}
		
		private function addConfigListeners(button:BubbleButton):void{
			button.HitArea.addEventListener(MouseEvent.MOUSE_OVER, configButtonMouseOver);
			button.HitArea.addEventListener(MouseEvent.MOUSE_OUT, configButtonMouseOut);
		}
		private function configButtonMouseOver(evt:MouseEvent):void{
			FisixContainer.fixed = true;
			if (_configButtonsTimer.running){
				_configButtonsTimer.reset();
			}
		}
		
		private function configButtonMouseOut(evt:MouseEvent):void{
			FisixContainer.fixed = false;
			_configButtonsTimer.start();
		}
		
		private function toggleLabel(evt:Event):void{
			ShowLabel = !ShowLabel;
			/*var button:BubbleButton = BubbleButton(evt.target);
			if (ShowLabel){
				button.Label = "Turn label off";
			}
			else{
				button.Label = "Turn label on";	
			}*/
		}
		
		private function changeColor(evt:Event):void{
			//Stop the bubble
			StopMoving = true;
			//Eliminate all annoying listeners
			removeAllListeners();
			for (var i:int=0; i < _configButtons.length; i++){
				var button:BubbleButton = _configButtons[i];
				removeConfigListeners(button);
				button.popOut();
			}
			
			if (_colorPicker!=null&&contains(_colorPicker)){
				removeChild(_colorPicker);
			}
			_colorPicker = new CustomColorPicker(BaseColor);
			_colorPicker.addEventListener(CustomColorPicker.CONFIRM_COLOR, confirmColor);
			addChild(_colorPicker);
			_colorPicker.addEventListener(CustomColorPicker.CANCEL_COLOR, cancelColor);
			_colorPicker.addEventListener(CustomColorPicker.CONFIRM_COLOR, confirmColor);
			
			_configButtonsTimer = null;
		}
		
		private function cancelColor(evt:Event):void{
			finishColorChanging();
		}
		
		private function confirmColor(evt:Event):void{
			BaseColor = _colorPicker.PickedColor;
			_colorPicker.destroy();
			finishColorChanging();
		}
		
		private function finishColorChanging():void{
			StopMoving = false;
			addAllListeners();
			for (var i:int=0; i < _configButtons.length; i++){
				var button:BubbleButton = _configButtons[i];
				addConfigListeners(button);
			}	
		}
		
		private function removeConfigButtons(evt:TimerEvent):void{
			for (var i:int = 0; i< _configButtons.length; i++) {
				var button:BubbleButton = _configButtons[i];
				button.popOut();
			}
		}
	}
}